<template>
  <transition name="food">
    <div class="food" v-show="foodFlag" ref="foodscroll">
      <div class="food-content">
        <div class="img-wrapper">
          <img class="img" :src="food.image" width="100%" height="100%">
          <div class="back" @click="hide">
            <i class="icon-arrow_lift"></i>
          </div>
        </div>
        <div class="content">
          <h1 class="title">{{food.name}}</h1>
          <div class="note">
            <span class="sell-count">月售{{food.sellCount}}份</span>
            <span class="rating">好评率{{food.rating}}%</span>
          </div>
          <div class="price">
            <span class="new-price">¥{{food.price}}</span>
            <span class="old-price" v-show="food.oldPrice">¥{{food.oldPrice}}</span>
          </div>
          <div class="buy-warpper">
            <div class="cartcontrol-wrapper" v-show="food.count>0">
              <cartcontrol :food="food"></cartcontrol>
            </div>
            <div class="add-cart" v-show="!food.count" @click.stop.prevent="addCart">加入购物车</div>
          </div>
        </div>
        <split v-show="food.info"></split>
        <div class="info" v-show="food.info">
          <h1 class="title">商品介绍</h1>
          <p class="text">{{food.info}}</p>
        </div>
        <split></split>
        <div class="ratings">
          <h1 class="title">商品评价</h1>
          <ratingselect :select-type="selectType" :only-content="onlyContent" :desc="desc" :ratings="food.ratings"></ratingselect>
          <div class="rating-wrapper">
            <ul v-show="food.ratings && food.ratings.length">
              <li v-show="needShow(rating.rateType,rating.text)" v-for="(rating, index) in food.ratings" :key="index" class="rating-item border-1px">
                <div class="time-user">
                  <div class="time">{{rating.rateTime | formatDate}}</div>
                  <div class="user">
                    <span class="username">{{rating.username}}</span>
                    <img class="avatar" :src="rating.avatar" width="12" height="12">
                  </div>
                </div>
                <div class="detail">
                  <i :class="{'icon-thumb_up':rating.rateType===0,'icon-thumb_down':rating.rateType===1}"></i>
                  <span class="text">{{rating.text}}</span>
                </div>
              </li>
            </ul>
            <div v-show="!food.ratings || !food.ratings.length" class="no-rating">暂无评价</div>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import cartcontrol from 'components/cartcontrol/cartcontrol';
import split from 'components/split/split';
import ratingselect from 'components/ratingselect/ratingselect';
import { formatDate } from '../../common/js/date.js';
import Vue from 'vue';
import BScroll from 'better-scroll';

// const POSITIVE = 0;
// const NEGATIVR = 1;
const ALL = 2;

export default {
  props: {
    food: {
      type: Object
    }
  },
  data () {
    return {
      foodFlag: false,
      selectType: ALL,
      onlyContent: true,
      desc: {
        all: '全部',
        positive: '推荐',
        negative: '吐槽'
      }
    };
  },
  methods: {
    show () {
      this.foodFlag = true;
      this.selectType = ALL;
      this.onlyContent = false;
      this.$nextTick(() => {
        if (!this.scroll) {
          this.scroll = new BScroll(this.$refs.foodscroll, {
            click: true
          });
        } else {
          this.scroll.refresh();
        }
      });
    },
    hide () {
      this.foodFlag = false;
    },
    addCart () {
      Vue.set(this.food, 'count', 1);
    },
    needShow (type, text) {
      this.$root.eventHub.$on('ratingtypeselect', (type) => {
        this.selectType = type;
        // this.$nextTick(() => {
        //   this.scroll.refresh();
        // });
      });
      this.$root.eventHub.$on('contentshow', (content) => {
        this.onlyContent = content;
        // this.$nextTick(() => {
        //   this.scroll.refresh();
        // });
      });
      
      if (this.onlyContent && !text) {
        return false;
      }
      if (this.selectType === ALL) {
        return true;
      } else {
        return type === this.selectType;
      }
    }
  },
  filters: {
    formatDate (time) {
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm');
    }
  },
  components: {
    cartcontrol,
    split,
    ratingselect
  }
};
</script>

<style lang="stylus" scoped="scoped">
@import '../../common/stylus/mixin.styl';
  .food
    position: fixed
    left: 0
    top: 0
    bottom: 48px
    width: 100%
    background: #fff
    transform: translate3d(0, 0, 0)
    &.food-enter-active, &.food-leave-active
      transition: all .5s
    &.food-enter, &.food-leave-to
      transform: translate3d(100%, 0, 0)
    .img-wrapper
      position: relative
      width: 100%
      height: 0
      padding-top: 100%
      .img
        position: absolute
        left: 0
        top: 0
      .back
        position: absolute
        left: 0
        top: 10px
        .icon-arrow_lift
          display: inline-block
          padding: 10px
          color: #fff
    .content
      position: relative
      padding: 18px
      .title
        margin-bottom: 8px
        line-height: 14px
        font-size: 14px
        font-weight: 700
        color: rgb(7, 17, 27)
      .note
        margin-bottom: 18px
        font-size: 0
        .sell-count, .rating
          font-size: 10px
          line-height: 10px
          color: rgb(147, 153, 159)
        .sell-count
          margin-right: 12px
      .price
        font-size: 0
        .new-price
          line-height: 24px
          font-size: 14px
          font-weight: 700
          color: rgb(240, 20, 20)
          margin-right: 12px
        .old-price
          line-height: 24px
          font-size: 10px
          color: rgb(147, 153, 159)
      .buy-warpper
        position: absolute
        right: 18px
        bottom: 18px
        // .cartcontrol-wrapper
        .add-cart
          padding: 0 12px
          height: 24px
          line-height: 24px
          font-size: 10px
          color: #fff
          background-color: rgb(0, 160, 220)
          border-radius: 12px
    .info
      padding: 18px
      .title
        font-size: 14px
        line-height: 14px
        margin-bottom: 6px
        color: rgb(7, 17, 27)
      .text
        padding: 0 6px
        font-size: 12px
        line-height: 24px
        color: rgb(77, 85, 93)
    .ratings
      padding-top: 18px
      .title
        font-size: 14px
        line-height: 14px
        margin-left: 18px
        color: rgb(7, 17, 27)
      .rating-item
        padding: 16px 0
        margin: 0 18px
        border-bottom-1px(rgba(7, 17, 27, .1))
        .time-user
          display: flex
          margin-bottom: 6px
          font-size: 10px
          height: 12px
          line-height: 12px
          color: rgb(147, 153, 159)
          .time
            flex: 1
          .user
            flex: 1
            text-align: right
            .username
              margin-right: 6px
            .avatar
              vertical-align: top
        .detail
          font-size: 12px
          .icon-thumb_down,.icon-thumb_up
            display: inline-block
            line-height: 24px
            margin-right: 4px
            &.icon-thumb_up
              color: rgb(0, 160, 220)
            &.icon-thumb_down
              color: rgb(147, 153, 159)
          .text
            line-height: 16px
            color: rgb(7, 17, 27)
      .no-rating
        padding: 16px 0
        margin-left: 18px
        font-size: 12px
        color: rgb(147, 153, 159)
</style>
